package com.old.service.Impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import com.old.service.OssService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service("ossService")
@PropertySource("classpath:application.properties")
public class OssServiceImpl implements OssService {
    @Value("${OSS.endpoint}")
    private String endpoint;
    @Value("${OSS.accessKeyId}")
    private String accessKeyId;
    @Value("${OSS.accessKeySecret}")
    private String accessKeySecret;
    @Value("${OSS.bucketName}")
    private String bucketName;


    public String uploadFile(MultipartFile multipartFile) {


        // 创建 OSSClient 对象。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            // 获取文件上传的流
            InputStream inputStream = multipartFile.getInputStream();

            // 构建存储目录
            String filename = multipartFile.getOriginalFilename();
            String path = buildPath(filename);

            // 设置上传类型
            ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setContentType(getcontentType(filename.substring(filename.lastIndexOf("."))));

            // 上传
            ossClient.putObject(bucketName, path, inputStream,objectMetadata);

            // 返回存储路径
            return "https://" + bucketName + "." + endpoint + "/" + path;
        } catch (Exception e) {
            return "ERROR";
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    public String uploadFile(File file) {
        // 创建 OSSClient 对象。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            // 获取文件上传的流
            InputStream inputStream = new FileInputStream(file);

            // 构建存储目录
            String filename = file.getName();
            String path = buildPath(filename);

            // 设置上传类型
            ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setContentType(getcontentType(filename.substring(filename.lastIndexOf("."))));

            // 上传
            ossClient.putObject(bucketName, path, inputStream,objectMetadata);

            // 返回存储路径
            return "https://" + bucketName + "." + endpoint + "/" + path;
        } catch (Exception e) {
            return "ERROR";
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }


    // 构建存储路径
    private String buildPath(String originalName) {
        String prefix = new SimpleDateFormat("yyyy/MM/dd").format(new Date());
        String suffix = originalName.substring(originalName.lastIndexOf("."));
        String filename = UUID.randomUUID().toString();
        String path = prefix + "/" + filename + suffix;

        return path;
    }



    private String getcontentType(String FilenameExtension) {
        if (FilenameExtension.equalsIgnoreCase(".bmp")) {
            return "image/bmp";
        }
        if (FilenameExtension.equalsIgnoreCase(".gif")) {
            return "image/gif";
        }
        if (FilenameExtension.equalsIgnoreCase(".jpeg") ||
                FilenameExtension.equalsIgnoreCase(".jpg") ||
                FilenameExtension.equalsIgnoreCase(".png")) {
            return "image/jpg";
        }
        if (FilenameExtension.equalsIgnoreCase(".html")) {
            return "text/html";
        }
        if (FilenameExtension.equalsIgnoreCase(".txt")) {
            return "text/plain";
        }
        if (FilenameExtension.equalsIgnoreCase(".vsd")) {
            return "application/vnd.visio";
        }
        if (FilenameExtension.equalsIgnoreCase(".pptx") ||
                FilenameExtension.equalsIgnoreCase(".ppt")) {
            return "application/vnd.ms-powerpoint";
        }
        if (FilenameExtension.equalsIgnoreCase(".docx") ||
                FilenameExtension.equalsIgnoreCase(".doc")) {
            return "application/msword";
        }
        if (FilenameExtension.equalsIgnoreCase(".xml")) {
            return "text/xml";
        }
        return "image/jpg";
    }

}
